<?php

namespace App\Http\Controllers;

use App\http\Model\Parking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class ChargeController extends Controller
{
    public function index(){
        $admin = session('admin');
        return view('home/charge/index')->with('admin',$admin);
    }

    public function modify(){
        $file = './config/charge.php';
        $charge = include $file;
        $input = Input::except('_token');
        $input['LASTTIME'] = time();
        $charge = array_merge($charge,array_change_key_case($input,CASE_UPPER));
        $str = "<?php\r\nreturn " .var_export($charge,true)."\r\n?>";
        if(file_put_contents($file,$str)){
            return response()->json(['code'=>1,'msg'=>'修改成功' ],200);
        }else{
            return response()->json(['code'=>1,'msg'=>'修改成功' ],200);
        }
    }

    public function bill(){//收费统计方法
        $m = date('m');
        $data = Parking::all()->toArray();//查出所有停车数据
        $tot = [];
        for ($i=1; $i <=$m ; $i++) {//按月统计
            $sum = 0;$car_num = 0;$bus_num = 0;$still = 0;
            foreach ($data as $k => $v) {
                if(date('m',$v['driveout_time'])==$i && $v['state']){
                    $sum += $v['charge'];
                    if($v['plate_type']==1){
                        $car_num += 1;
                    }elseif($v['plate_type']==2){
                        $bus_num += 1;
                    }
                }elseif(date('m',$v['drivein_time'])==$i && !$v['state']){
                    $still += 1;
                }
            }
            $tot[$i]['sum'] = $sum;
            $tot[$i]['car_num'] = $car_num;
            $tot[$i]['bus_num'] = $bus_num;
            $tot[$i]['still'] = $still;

        }
        return view('home/charge/bill')->with('tot',$tot);
    }
}
